package admin.controllers;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.OrderModel;
import models.ProfitBySaler;

import search.OrderSearchData;

@SuppressWarnings("serial")
public class SaleProfitBySalerServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(SaleProfitBySalerServlet.class.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		process(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		try {
			boolean toFile = false;
			String action = request.getParameter("action");
			if (action != null && action.equals("toFile")) {
				toFile = true;
			}
			String strCity = request.getParameter("city");
			String strFromDate = request.getParameter("fromDate");
			String strToDate = request.getParameter("toDate");
			Date fromDate;
			Date toDate;
			DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
			try {
				fromDate = formatter.parse(strFromDate);
				request.setAttribute("fromDate", strFromDate);
			} catch (Exception e) {
				fromDate = null;
			}
			try {
				toDate = formatter.parse(strToDate);
				request.setAttribute("toDate", strToDate);
			} catch (Exception e) {
				toDate = null;
			}
			if (strCity != null && !strCity.isEmpty()) {
				request.setAttribute("city", strCity);
			}
			OrderSearchData orderSearch = new OrderSearchData();
			orderSearch.setLimit(500);
			orderSearch.searchAdvanceByCheckOutTime(strCity, fromDate, toDate);
			List<OrderModel> listResult = orderSearch.getListResult();
			TreeMap treeProfitSaler = new TreeMap();
			for (OrderModel orderModel : listResult) {
				if (treeProfitSaler.get(orderModel.getSalername()) != null) {
					if (orderModel.getSalername() != null
							&& !orderModel.getSalername().equals("")) {
						ProfitBySaler profit = (ProfitBySaler) treeProfitSaler
								.get(orderModel.getSalername());
						if (orderModel.getStatus() != 5) {
							profit.setDisplayGP(profit.getDisplayGP()
									+ orderModel.getTotalPrice());
						}
						if (orderModel.getStatus() == 4) {
							profit.setRealGP(profit.getRealGP()
									+ orderModel.getTotalPrice());
						}
						treeProfitSaler.put(orderModel.getSalername(), profit);
					}
				} else {
					if (orderModel.getSalername() != null
							&& !orderModel.getSalername().equals("")) {
						ProfitBySaler profit = new ProfitBySaler();
						profit.setSalername(orderModel.getSalername());
						if (orderModel.getStatus() != 5) {
							profit.setDisplayGP(orderModel.getTotalPrice());
						}
						if (orderModel.getStatus() == 4) {
							profit.setRealGP(orderModel.getTotalPrice());
						}
						treeProfitSaler.put(orderModel.getSalername(), profit);
					}
				}
			}

			List<ProfitBySaler> listProfit = new ArrayList<ProfitBySaler>();
			Iterator entries = treeProfitSaler.entrySet().iterator();
			while (entries.hasNext()) {
				Entry thisEntry = (Entry) entries.next();
				Object value = thisEntry.getValue();
				ProfitBySaler profit = (ProfitBySaler) value;
				listProfit.add(profit);
			}
			if (listProfit.size() == 0) {
				listProfit.add(new ProfitBySaler());
			}
			request.setAttribute("result", listProfit);
			if (toFile) {
				request.getRequestDispatcher(
						"/admin/pages/file_sale_profit_by_saler.jsp").forward(
						request, response);
			} else {
				request.getRequestDispatcher(
						"/admin/pages/sale_profit_by_saler.jsp").forward(
						request, response);
			}
		} catch (ServletException e) {
			System.out.println("Error when try to get sale profit !");
			log.warning("Error when try to get sale profit !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
